﻿Imports MySql.Data
Imports MySql.Data.MySqlClient

Public Class MySQLConnect
    ''' <summary>
    ''' Objet de type MySqlConnection contenant la connexion à la BDD
    ''' </summary>
    Public Shared maconnexion As New MySqlConnection
    ''' <summary>
    ''' Permet d'exécuter des requêtes SQL
    ''' </summary>
    Public Shared macommandespecialerequete As New MySqlClient.MySqlCommand
    ''' <summary>
    ''' Permet de manipuler les données
    ''' </summary>
    Public Shared monremplisseur As New MySqlClient.MySqlDataAdapter
    ''' <summary>
    ''' Permet de gérer un tableau de données
    ''' </summary>
    Public Shared monjeudedonnees As New Data.DataSet
    ''' <summary>
    ''' Permet de faire des requêtes
    ''' </summary>
    Public Shared marequete As String
    ''' <summary>
    ''' Chaîne de de connexion à la base de données
    ''' </summary>
    Public Shared machaine As String = "server=localhost;user id=root;password=;database=iapm_sp2"

    ''' <summary>
    ''' Se connecte à la base de données
    ''' </summary>

    Public Shared Sub SeConnecter()
        MaConnexion.ConnectionString = Machaine
        MaConnexion.Open()
        MonJeuDeDonnees = New Data.DataSet("IAPM")
        monjeudedonnees.Clear()
        MaCommandeSpecialeRequete.CommandType = CommandType.Text
        MaCommandeSpecialeRequete.Connection = MaConnexion
        'Dim MyCommand As SqlCommand = MaConnexion.CreateCommand()
    End Sub
    ''' <summary>
    ''' Génère la clé primaire. Retourne l'id +1
    ''' </summary>
    ''' <param name="nomcle">Nom de la clé à utiliser</param>
    ''' <param name="nomtable">Nom de la table ou generer la clé primaire</param>
    Public Shared Function GenererClePrimaire(ByVal nomcle As String, ByVal nomtable As String) As Integer
        Dim resultat As Object
        Dim cle As Integer
        MySQLConnect.marequete = "SELECT MAX(" & nomcle & ") FROM " & nomtable
        MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
        resultat = MySQLConnect.macommandespecialerequete.ExecuteScalar()

        If IsDBNull(resultat) Then
            cle = 0
        Else
            cle = resultat
        End If
        Return cle + 1

    End Function


    Public Shared Sub Initialiser_TextBoxs(ByRef unefeuille As Form)
        Dim txt As Object
        For Each txt In unefeuille.Controls
            If TypeOf (txt) Is TextBox Then
                txt.Text = ""
            End If
        Next
    End Sub

    Public Shared Function RemplaceApostrophesSlash(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("'", "/")
        Return ch
    End Function
    Public Shared Function RemplaceSlashApostrophes(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("/", "'")
        Return ch
    End Function

    Public Shared Function RemplaceSlashApostrophesDataTable(ByRef lesdonnees As DataTable)
        Dim nblignes As Integer
        Dim nbcolonnes As Integer
        Dim test As String = ""
        nblignes = lesdonnees.Rows.Count
        nbcolonnes = lesdonnees.Columns.Count
        Dim i, j As Integer
        For i = 0 To nblignes - 1
            For j = 0 To nbcolonnes - 1
                'Attention le replace ne s'applique qu'aux type String : utilisation de la classe de base Object :
                If Object.ReferenceEquals(lesdonnees.Rows(i).Item(j).GetType(), test.GetType()) Then
                    lesdonnees.Rows(i).Item(j) = RemplaceSlashApostrophes(lesdonnees.Rows(i).Item(j))
                End If
            Next
        Next
        Return lesdonnees
    End Function
    Public Shared Function RemplaceSlashApostrophesDataTableCommande(ByRef lesdonnees As DataTable)
        Dim nblignes As Integer
        Dim nbcolonnes As Integer
        Dim test As String = ""
        nblignes = lesdonnees.Rows.Count
        nbcolonnes = lesdonnees.Columns.Count
        Dim i As Integer
        For i = 0 To nblignes - 1

            'Attention le replace ne s'applique qu'aux type String : utilisation de la classe de base Object :
            If Object.ReferenceEquals(lesdonnees.Rows(i).Item(2).GetType(), test.GetType()) Then
                lesdonnees.Rows(i).Item(2) = RemplaceSlashApostrophes(lesdonnees.Rows(i).Item(2))
            End If
        Next

        Return lesdonnees
    End Function


End Class
